<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="keywords" content="关键字">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="descrition" content="网页描述">
    <title>网页标题</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .scope {
            width: 600px;
            height: 600px;
            margin: 0px auto;
            background-color: aqua;
            position: relative;
        }

        .menu {
            text-align: center;
            width: 600px;
            margin: 20px auto;
        }
    </style>
    <head>
    <body>
        <div class="menu">
            <button id="start">开始游戏</button>
            <!-- <span>分数：</span> -->
        </div>
        <div class="scope"></div>
        <script src="./food.js">
        </script>
        <script src="./snake.js">
        </script>
        <script>
            //获取整个地图
            let scope = document.querySelector(".scope")
            //定义移动的方向
            let direction = 'right'
            //实例化食物
            let food = new Food()
            let foodArr = []
            food.init(scope)
            //实例化蛇
            let snake = new Snake(20, 20, direction)
            let snakeArr = []
            snake.init(scope)
            //实例化蛇的函数
            function InitSnake(){
                remove(snakeArr)
                snake = null
                snakeArr = []
                snake = new Snake(20, 20, direction)
                snake.init(scope)
            }
            function remove(array) {
                if (array.length == 0) return
                for (let i = 0; i < array.length; i++) {
                    //删除地图中的节点
                    array[i].parentNode.removeChild(array[i])
                }
                //删除数组中的节点
                array.length = []
            }
            let start = document.querySelector("#start")
            let timer
            function StartGame() {
                timer = setInterval(() => {
                    if(snake.stop(scope)){
                        start.onclick = ()=>{
                            StartGame()
                            InitSnake()
                        }
                        
                        // alert("游戏结束")
                        clearInterval(timer)
                    }else{
                        snake.move(scope, food, snake)
                    }
                }, 150)
                start.onclick = null
            }
            //绑定开始事件
            start.onclick = ()=>{
                StartGame()
                InitSnake()
            }
            //绑定按键事件
            document.onkeydown = (evt) => {
                if (evt.keyCode == 37 && snake.direction !== 'right') {
                    snake.direction = 'left'
                    // return false
                }
                if (evt.keyCode == 38 && snake.direction !== 'down') {
                    snake.direction = 'up'
                }
                if (evt.keyCode == 39 && snake.direction !== 'left') {
                    snake.direction = 'right'
                }
                if (evt.keyCode == 40 && snake.direction !== 'up') {
                    snake.direction = 'down'
                }
            }
        </script>
        <!--<script src="vue3/vue.js"></script>-->
    </body>

</html>